package com.test.ai.controller;

import com.test.ai.dto.ChatRequest;
import com.test.ai.service.HistroyPersonChatService;
import com.test.ai.utils.FluxUtils;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 8/21/25 8:53 AM
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
public class FormatResponseController {
  @Autowired private HistroyPersonChatService histroyPersonChatService;

  /**
   * Chat stream mono.
   *
   * @param request the request
   * @return the mono
   */
  @PostMapping(value = "/history/chat", produces = "application/json;charset=UTF-8")
  public Mono<Map> chatStream(@RequestBody ChatRequest request) {
    final Flux<String> flux =
        histroyPersonChatService.generateResponse(request.message(), request.conversationId());
    // 1. 将json字符串合并为一个完整的json字符串
    final Mono<String> completeMonoJson = flux.reduce("", (s, t) -> s + t);
    // 2. 将响应转换为map对象
    return FluxUtils.revertJson(Map.class, completeMonoJson);
  }
}
